package com.tbit.main.controller;

import cn.hutool.core.date.DateUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.tbit.main.common.JsonResult;
import com.tbit.main.constant.BrandRemoteConfigConstant;
import com.tbit.main.pojo.BrandRemoteConfig;
import com.tbit.main.pojo.OperaPsw;
import com.tbit.main.service.AccountService;
import com.tbit.main.service.BrandRemoteConfigService;
import com.tbit.main.service.OperaPswService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.constraints.NotNull;
import java.util.List;
import java.util.Objects;

/**
 * 操作密钥控制器
 */
@Validated
@RestController
@RequestMapping("/operaPsw")
public class OperaPswController {

    @Autowired
    private OperaPswService operaPswService;
    @Autowired
    private AccountService accountService;
    @Autowired
    private BrandRemoteConfigService brandRemoteConfigService;

    /**
     * 根据模块key查询操作密钥
     *
     * @param brandId 品牌id
     * @param module  模块key
     */
    @GetMapping("/getByModule")
    public JsonResult<OperaPsw> getByModule(@NotNull Integer brandId, @NotNull Integer module) {
        OperaPsw operaPsw = operaPswService.getOperaPswByModule(brandId, module);
        return JsonResult.succ(operaPsw);
    }

    /**
     * 根据区域id和模块查询操作密钥
     *
     * @param accountId 区域id
     * @param module    模块key
     */
    @GetMapping("/getByModuleAndAccountId")
    public JsonResult<OperaPsw> getByModuleAndAccountId(@NotNull Integer accountId, @NotNull Integer module) {
        Integer brandId = accountService.getByAccount(accountId);
        OperaPsw operaPsw = operaPswService.getOperaPswByModule(brandId, module);
        return JsonResult.succ(operaPsw);
    }

    /**
     * 根据区域id查询操作密钥总开关
     *
     * @param accountId 区域id
     */
    @GetMapping("/getEnableByAccountId")
    public JsonResult<Boolean> getEnableByAccountId(@NotNull Integer accountId) {
        Integer brandId = accountService.getByAccount(accountId);
        BrandRemoteConfig brandRemoteConfig = brandRemoteConfigService.getByAccountIdAndType(brandId, BrandRemoteConfigConstant.OPERA_PSW);
        if (brandRemoteConfig == null) {
            BrandRemoteConfig brandRemoteConfigDefault = new BrandRemoteConfig();
            brandRemoteConfigDefault.setAccountId(brandId);
            brandRemoteConfigDefault.setConfigType(BrandRemoteConfigConstant.OPERA_PSW);
            brandRemoteConfigDefault.setParamKV("1");
            brandRemoteConfigDefault.setUpdateTime(DateUtil.now());
            brandRemoteConfigDefault.setRemark("品牌密钥总开关");
            brandRemoteConfigService.insert(brandRemoteConfigDefault);
            brandRemoteConfig = brandRemoteConfigDefault;
        }
        return JsonResult.succ(Objects.equals(brandRemoteConfig.getParamKV(), "1"));
    }

    /**
     * 操作密钥分页查询
     *
     * @param accountId 区域id
     * @param pageNo   页码
     * @param pageSize 条数
     */
    @GetMapping("/queryPage")
    public JsonResult queryPage(@NotNull Integer accountId, @NotNull Integer pageNo, @NotNull Integer pageSize) {
        Integer brandId = accountService.getByAccount(accountId);
        PageHelper.startPage(pageNo, pageSize);
        List<OperaPsw> operaPswList = operaPswService.queryPage(brandId);
        PageInfo<OperaPsw> pageInfo = new PageInfo(operaPswList);
        return JsonResult.succ((int) pageInfo.getTotal(), pageInfo.getList());
    }
}
